let app = getApp()
Page({
    /**
     * 页面的初始数据
     */
    data: {
        spinShow: false,
        //tab active属性
        current: 0,
        // status 订单状态
        status: 0,
        orderUnFinishNum: 0,
        orderFinishedNum: 0,
        orderCancelNum: 0,
        loadMoreLoad: false,
        loadMoreTip: '',
        queryParam: {
            status: 0,
            pageNum: 1,
            pageSize: app.globalData.pageSize
        },
        tableData: [],
        unFinishData: [],
        finishedData: [],
        cancelData: [],
        pages: 0
    },

    handleChange({detail}) {
        let _this = this
        let tableData = []
        let status = detail.name
        let index = detail.index
        if (status == 1) {
            tableData = this.data.finishedData
        } else if (status == 2) {
            tableData = this.data.cancelData
        } else {
            tableData = this.data.unFinishData
        }
        _this.setData({
            current: index,
            status: status,
            tableData: tableData,
            queryParam: {
                status: status,
                pageNum: 1,
                pageSize: app.globalData.pageSize
            },
        });
        this.queryData(true)
    },
    onPullDownRefresh() {
        this.setData({
            spinShow: true
        });
        if (!this.loading) {
            this.setData({
                ['queryParam.pageNum']: 1,
                loadMoreLoad: false,
                loadMoreTip: '正在刷新',
            });
            this.queryData(true)
        }
    },
    onReachBottom() {
        if (!this.loading && this.data.queryParam.pageNum < this.data.pages) {
            this.setData({
                loadMoreLoad: true,
                loadMoreTip: '正在加载'
            });
            this.setData({
                ['queryParam.pageNum']: this.data.queryParam.pageNum + 1
            });
            this.queryData(false)
        }
    },
    queryData: function (override) {
        let _this = this
        app.formGet('/app/order/page', this.data.queryParam).then(res => {
            _this.setData({
                spinShow: false
            });
            wx.stopPullDownRefresh()
            if (res.code === 0) {
                let tableData = override ? res.data.list : this.data.tableData.concat(res.data.list)
                let url = ""
                if (this.data.queryParam.status == 1) {
                    url = "/pages/order/finish/index?orderId="
                } else if (this.data.queryParam.status == 2) {
                    url = "/pages/order/cancel/index?orderId="
                } else {
                    url = "/print-package/pages/order/prepare/index?orderId="
                }

                for (let i = 0; i < tableData.length; i++) {
                    tableData[i].url = url + tableData[i].id
                }
                _this.setData({
                    tableData: tableData,
                    pages: res.data.pageInfo.pages
                });
                if (this.data.queryParam.status == 0) {
                    _this.setData({
                        orderUnFinishNum: res.data.pageInfo.total
                    });
                }
                if (this.data.queryParam.pageNum >= res.data.pageInfo.pages) {
                    this.setData({
                        loadMoreLoad: false,
                        loadMoreTip: '暂无更多数据'
                    });
                }
            } else if (res.code == 999) {
                app.message(res.message, 'error')
            } else if (res.code == -1) {
                app.message(res.message, 'error')
            } else {
                app.message(res.message, 'error')
            }
        }).catch(e => {
            _this.setData({
                spinShow: false
            });
            app.message(e, 'error')
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
    },
    onShow: function () {
        let _this = this
        this.setData({
            spinShow: true,
            queryParam: {
                status: _this.data.status,
                pageNum: 1,
                pageSize: app.globalData.pageSize
            },
            loadMoreLoad: false,
            loadMoreTip: ''
        });
        this.queryData(true)
    }


})
